package com.xtkj.superclub_shop.controller;

import com.xtkj.commons.JsonResult;
import com.xtkj.commons.SnowflakeIdWorker;
import com.xtkj.superclub_entity.entity.shop.Address;
import com.xtkj.superclub_shop.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lizheqi
 * @create 2019-12-30-16:23
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private IAddressService addressService;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 增加地址
     * @param address
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JsonResult addAddress(@RequestBody Address address, HttpServletRequest request){
        //通过请求头拿到用户的userId
        String userId = "262627114774167552";
        //String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            address.setAddressId(snowflakeIdWorker.nextId()+"").setUserId(userId);
            addressService.addAddress(address);
            return JsonResult.success("增加地址成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 修改地址
     * @param address
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public JsonResult updateAddress(Address address, HttpServletRequest request){
        //通过请求头拿到用户的userId
        String userId = "262627114774167552";
        //String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            addressService.updateAddress(address);
            return JsonResult.success("修改地址成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 删除地址
     * @param address
     * @param request
     * @return
     */
    @RequestMapping(value = "/del",method = RequestMethod.DELETE)
    public JsonResult delAddress(Address address, HttpServletRequest request){
        //通过请求头拿到用户的userId
        String userId = "262627114774167552";
        //String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            addressService.delAddress(address);
            return JsonResult.success("删除地址成功").setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }

    /**
     * 查询所有地址
     * @param
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult listAddress(HttpServletRequest request){
        //通过请求头拿到用户的userId
        String userId = "262627114774167552";
        //String userId = TokenUtil.getUserId(request.getHeader("token"));
        try {
            List<Address> addressList = addressService.listAddress(userId);
            return JsonResult.success("查询得到所有该用户地址").setCode(200).setData(addressList);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage()).setCode(400);
        }
    }
}
